<?php

$classDir = 'classes/';
include $classDir . 'conversation.php';
include $classDir . 'protocolObject.php';
include $classDir . 'user.php';
include $classDir . 'group.php';


function sendDebugInfo($outputObject)
{
	$sqlApprovedObject = urlencode(json_encode($outputObject));
	if(!isset($outputObject->messageSender))
	{
		crowFather("insert into debug(senderId,returnObject) values(3,'" . $sqlApprovedObject . "');",3);
	}
	else
	{
		crowFather("insert into debug(senderId,returnObject) values(" . $outputObject->messageSender . ",'" . $sqlApprovedObject . "');",3);
	}
}

function checkUsersExist($outputObject)
{
	if((isset($outputObject->messageSender) || isset($outputObject->messageSenderID)) && (isset($outputObject->messageRecipient) || $outputObject->messageRecipientID))
	{
		$outputObject->userExistBool = array();
		$outputObject->userDupBool = array();
		$outputObject->senderQueryResult = crowFather("select count(*) from user where userId = '" . $outputObject->messageSender . "';",5);
		$outputObject->recipientQueryResult = crowFather("select count(*) from user where userId = '" . $outputObject->messageRecipient . "';",5);
		
		switch($outputObject->senderQueryResult)
		{
			case 0:
				#No user exists
				$outputObject->userExistBool["sender"] = false;
				$outputObject->userDupBool["sender"] = false;
				break;
			case 1:
				#User exists
				$outputObject->userExistBool["sender"] = true;
				$outputObject->userDupBool["sender"] = false;
				break;
		}
		if($outputObject->senderQueryResult > 1)
		{
			#User exists but is duplicated
			$outputObject->userExistBool["sender"] = true;
			$outputObject->userDupBool["sender"] = true;
		}
		
		switch($outputObject->recipientQueryResult)
		{
			case 0:
				#No user exists
				$outputObject->userExistBool["recipient"] = false;
				$outputObject->userDupBool["recipient"] = false;
				break;
			case 1:
				#User exists
				$outputObject->userExistBool["recipient"] = true;
				$outputObject->userDupBool["recipient"] = false;
				break;
		}
		if($outputObject->recipientQueryResult)
		{
			#User exists but is duplicated
			$outputObject->userExistBool["recipient"] = true;
			$outputObject->userDupBool["recipient"] = true;
		}
	}
	
	else
	{
		#if this dosen't evaluate , no user is set , so make return 0
		
	}
}

function dataFieldConstructor($Array,$constructId)
{
	if($Array != null)
	{
		if($constructId == 1)
		{
			$return = "<select>";
			foreach($Array as $item)
			{
				$return .= "<option data-option='" . $item  . "' onclick='setGroupId(" . $item . ");' >" . $item . "</option>";
			}
			$return .= "</select>";
		}
		
		return $return;
	}
	else
	{
		return "The data was not in a correct format or was not set"; 
	}
}
?>